//
//  DependentComponentPickerViewController.m
//  Pickers
//
//  Created by KaiKai on 11-7-18.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "DependentComponentPickerViewController.h"


@implementation DependentComponentPickerViewController

@synthesize picker;
@synthesize stateZips;
@synthesize states;
@synthesize zips;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [picker release];
    [stateZips release];
    [states release];
    [zips release];
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    NSBundle *bundle = [NSBundle mainBundle];
    NSString *plistPath = [bundle pathForResource:@"statedictionary" ofType:@"plist"];
    
    NSDictionary *dic = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
    self.stateZips = dic;
    [dic release];
    
    NSArray *components = [stateZips allKeys];
    self.states = [components sortedArrayUsingSelector:@selector(compare:)];
    NSString *selectedState = [states objectAtIndex:0];
    self.zips = [stateZips objectForKey:selectedState];
}

- (void)viewDidUnload
{
    [picker release];
    picker = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 2;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if (component == 0)
    {
//        NSInteger statecount = [states count];
        return [states count];
//        return 0;
    }
    
    return [zips count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    if (component == 0)
    {
        return [states objectAtIndex:row];
    }
    
    return [zips objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if (component == 0)
    {
        NSString *selectedState = [states objectAtIndex:row];
        self.zips = [stateZips objectForKey:selectedState];
        [picker selectRow:0 inComponent:1 animated:YES];
        [picker reloadComponent:1];
    }
}

- (IBAction)buttonPressed:(id)sender
{
    NSInteger stateRow = [picker selectedRowInComponent:0];
    NSInteger zipRow = [picker selectedRowInComponent:1];
    
    NSString *state = [states objectAtIndex:stateRow];
    NSString *zip = [zips objectAtIndex:zipRow];
    NSString *title = [[NSString alloc] initWithFormat:@"You selected zip code %@.", zip];
    NSString *message = [[NSString alloc] initWithFormat:@"%@ is in %@", zip, state];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alert show];
    [alert release];
    [title release];
    [message release];
}

@end
